import { Component, OnInit, AfterViewInit, ViewChild } from '@angular/core';
import { CommService } from './../../comm/comm.service';
import { Http, Response } from '@angular/http';
import { Router } from '@angular/router';

import 'rxjs/add/operator/map';
import { Subject } from 'rxjs/Rx';
import { Observable } from 'rxjs/Rx';

import { ReportUrls } from '../../comm/report.urls.conf';
import { SubArea } from '../../model/report/sub-area';
import { QueryModel } from '../../model/report/query-model';
import { ResponseModel } from '../../model/comm/response-model';

declare const $: any;
import * as moment from 'moment';
import { TVResource } from 'app/ptpmc/model/comm/tv-resource';
import { DxDataGridComponent } from '../../../../../node_modules/devextreme-angular';
import { AssistCallSummary } from 'app/ptpmc/model/report/assist-call-summary';
import { DxDropDownBoxComponent} from 'devextreme-angular';
declare var swal: any;

@Component({
    selector: 'app-assist-call-summary',
    templateUrl: './assist-call-summary.component.html',
    styleUrls: ['./assist-call-summary.component.css'],
})

export class AssistCallSummaryComponent implements OnInit, AfterViewInit {

    @ViewChild(DxDataGridComponent) dataGrid: DxDataGridComponent;
    /**查询按类型 */
    public typeItems = [{ id: 1, text: '按值统计' }, { id: 2, text: '按班次统计' }];
    public typeSelected = this.typeItems[0];

    /**选择项 */
    public items:AssistCallSummary[] =[];
    public selectedItems: AssistCallSummary[] = [];


    /*生产线 - 班次*/
    public subAreaItems: SubArea[];
    public shiftItems: any[];
    public equipemntItems: TVResource[] = [];

    public subAareSelected: any;
    public shiftSelected:any;
    public equipmentSelected:TVResource[]=[];

    constructor(private commServices: CommService) {
    }
    onSubAreaSelectedChange(subArea:any):void{
        this.equipemntItems = this.commServices.getTVResourceBySubArea(subArea.value.subArea);
        this.equipmentSelected = this.equipemntItems;
        //this.equipmentSelected = this.equipemntItems;
    }
    ngOnInit() {
        this.commServices.initDatePicker($('#DateTimeRange'),'YYYY-MM-DD HH:mm');
        this.shiftItems = this.commServices.shifts;
        this.shiftSelected = this.shiftItems[0];
        this.subAreaItems = this.commServices.getSubAreas();
        this.subAareSelected = this.subAreaItems[0];
        this.equipemntItems = this.commServices.getTVResourceBySubArea(this.subAreaItems[0].subArea);
        this.equipmentSelected = this.equipemntItems;
    }

    ngAfterViewInit() {
    }

    /**获取表格数据*/
    getReportData(): void {
        debugger;
        const queryModel: QueryModel = new QueryModel();
        queryModel.dateTimeRange = this.getTime('DateTimeRange');
        queryModel.querySubAreaIds = [this.subAareSelected.subArea];
        queryModel.queryResourceIds = this.equipmentSelected.map(e => e.resource);
        queryModel.shift = this.shiftSelected.shiftId;
        queryModel.updateType = this.typeSelected.id.toString();
        const url :string = ReportUrls.AssistFilterUrl + "/" + this.typeSelected.id.toString();
        this.commServices
            .getList(queryModel,url)
            .subscribe(model => {
                this.items = [];
                if (model.resultCode === 200) {
                    this.items = model.data;
                } else {
                    console.log(model.resultCode + ',' + model.resultMessage);
                }
            });
    }
    public updateFilterList:AssistCallSummary[] = [];
    onCheckBoxChange(entity: AssistCallSummary): void {
        let index:number = this.items.findIndex(e=>e.timestamp===entity.timestamp && e.subArea===entity.subArea && e.equipment===entity.equipment
            && e.shiftDay===entity.shiftDay && e.shiftId===entity.shiftId && e.resource===entity.resource);
        this.items
        if(index!=-1)
        {
            this.items[index].reportFilter = entity.reportFilter===1 ? 0:1;
            let upIndex:number = this.updateFilterList.findIndex(e=>e.timestamp===entity.timestamp && e.subArea===entity.subArea && e.equipment===entity.equipment
                && e.shiftDay===entity.shiftDay && e.shiftId===entity.shiftId && e.resource===entity.resource);
            if(upIndex!=-1)
            {
                this.updateFilterList[upIndex]=this.items[index];
            }
            else
            {
                this.updateFilterList.push(this.items[index]);
            }
        }
    }
    saveChanges(){
        this.commServices.update(this.updateFilterList,ReportUrls.AssistFilterUpdateUrl + "/1")
        .subscribe(model => {
            if (model.resultCode === 200) {
                this.updateFilterList=[];
                alert("更新成功！");
            } else {
                // console.log(model.resultCode + ',' + model.resultMessage);
                alert('更新失败！' + model.resultMessage);
            }});
    }
    /**获取TimeRange的值*/
    getTime(id: any): String {
        const timerange: String = $('#' + id).val();
        return timerange;
    }
    @ViewChild("dddb") dddb: DxDropDownBoxComponent;
    clearEquipmentSelected(){
        this.equipmentSelected=[];
    }
    equipmentSelectedOk()
    {
        this.dddb.instance.close();
    }
}
